package com.example.netty.http_demo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 初始化容器，channel 注册后，会执行相应的初始化方法
 */
public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //通过SocketChannel获取对应的管道
        ChannelPipeline pipeline=socketChannel.pipeline();

        //通过管道添加handler
        // HttpServerCodec是有netty自己提供的助手类，可以理解为拦截器
        //当请求到我们的服务器端，我们需要解码，相应到客户端我们需要编码
        pipeline.addLast("HttpServerCodec",new HttpServerCodec());

        //添加自定义的助手类，返回 Hello Netty
        pipeline.addLast("customHandler",new CustomHandler());

    }
}
